//
//  DownloadListView.swift
//  Trans
//
//  Created by kologe on 2024/10/19.
//

import SwiftUI

struct DownloadListView: View {
    @ObservedObject var dataArr = KzMutableArray<FileTransMessage>(array: [])
    @Binding var status : Int
    @State var fileName :String = ""
    
    var body: some View {
        GeometryReader{ geometry in
            VStack{
                if 1 == status{
                    Text(NSLocalizedString("downloadStatus", comment: "default")).titleStyle()
                }
                ZStack{
                    ScrollView(){
                        LazyVStack {
                            let reversedIndices = dataArr.array.indices.reversed()
                            ForEach(reversedIndices, id: \.self) { index in
                                VStack{
                                    KzProgressView(fileTransMesage: $dataArr.array[index],status: $status)
                                        .frame(width: geometry.size.width)
                                        .onTapGesture {
                                            let tapName = dataArr.array[index].absolutePath ?? ""
                                            if fileName == tapName {
                                                fileName = ""
                                            }else {
                                                fileName = tapName
                                            }
                                        }
                                    Divider()
                                        .background(kcolors.lineColor)
                                        .padding(.horizontal,0).offset(y:15)
                                }
                            }
                        }
                    }
                    if !fileName.isEmpty,1 == status{
                        VStack{
                            Spacer()
                            KLineViewCtl( absolutePath: $fileName ).frame(height: geometry.size.height/2).transition(.move(edge:.leading))
                        }
                    }
                }
                
            }.onDisappear(){
                fileName = ""
            }
        }
    }
}

#Preview {
    DownloadListView(status:.constant(0))
}
